package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.organization.util.IssueFeePolicyLimit;
import com.turing.post.organization.util.IssueFeePolicyLimitDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * 发卡商资费上下限数据访问对象
 * 
 * @author liun
 * @date 2013-05-27
 */
@Stateless(mappedName = "organization.impl.IssueFeePolicyLimitDaoImpl")
@Remote
@Local
public class IssueFeePolicyLimitDaoImpl implements IssueFeePolicyLimitDao {

	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	/**
	 * 新增发卡商资费上下限信息
	 * 
	 * @param entity
	 */
	@Override
	public void addIssueFeePolicyLimit(IssueFeePolicyLimit entity)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, entity);
	}

	/**
	 * 根据发卡商号和业务类型获取资费上下限信息
	 * 
	 * @param issueCompnyNo
	 * @param businessType
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public IssueFeePolicyLimit getIssueFeePolicyLimitByCompNoAndType(
			String issueCompnyNo, String businessType) {
		String sql = "SELECT I.ISSUECOMPANYNO, I.BUSINESSTYPE, I.FEEUPPERLIMIT, "
				+ "I.FEELOWERLIMIT, I.OPERID, I.MODIFYTIME, I.REMARK, I.STATUS, I.LASTAUTHOPERID, I.LASTAUTHTIME, I.AUTHREMARK "
				+ "FROM ISSUEFEEPOLICYLIMIT I "
				+ "WHERE I.ISSUECOMPANYNO = ?1 AND BUSINESSTYPE = ?2";
		Query query = _em.createNativeQuery(sql, IssueFeePolicyLimit.class);
		query.setParameter(1, issueCompnyNo);
		query.setParameter(2, businessType);
		List<IssueFeePolicyLimit> list = query.getResultList();
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 根据发卡商号获取发卡商各业务资费上下限列表
	 * 
	 * @param issueCompnyNo
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<IssueFeePolicyLimit> getIssueFeePolicyLimitListByCompNo(
			String issueCompnyNo, String businessType, Integer page) {
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "SELECT I.ISSUECOMPANYNO, I.BUSINESSTYPE, I.FEEUPPERLIMIT, "
				+ "I.FEELOWERLIMIT, I.OPERID, I.MODIFYTIME, I.REMARK, I.STATUS, I.LASTAUTHOPERID, I.LASTAUTHTIME, I.AUTHREMARK "
				+ "FROM ISSUEFEEPOLICYLIMIT I " 
				+ "WHERE I.ISSUECOMPANYNO = ?1 ";
		if(!PCPSUtil.isEmpty(businessType)){
			sql = sql + "AND I.BUSINESSTYPE = '" + businessType + "' ";
		}
		sql = sql + "ORDER BY I.MODIFYTIME DESC";
		Query query = _em.createNativeQuery(sql, IssueFeePolicyLimit.class).setFirstResult(startPosition).setMaxResults(maxResult);
		query.setParameter(1, issueCompnyNo);
		return query.getResultList();
	}

	/**
	 * 修改发卡商资费上下限信息
	 * 
	 * @param entity
	 */
	@Override
	public void modifyIssueFeePolicyLimit(IssueFeePolicyLimit entity)
			throws PCPSApplicationException {
		GeneralDao.update(_em, entity);
	}

	/**
	 * 修改前锁定表
	 * 
	 * @param issueCompnyNo
	 * @param businessType
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public IssueFeePolicyLimit getIssueFeePolicyLimitForUpdate(
			String issueCompnyNo, String businessType) {
		String sql = "SELECT I.ISSUECOMPANYNO, I.BUSINESSTYPE, I.FEEUPPERLIMIT, "
				+ "I.FEELOWERLIMIT, I.OPERID, I.MODIFYTIME, I.REMARK, I.STATUS, I.LASTAUTHOPERID, I.LASTAUTHTIME, I.AUTHREMARK "
				+ "FROM ISSUEFEEPOLICYLIMIT I "
				+ "WHERE I.ISSUECOMPANYNO = ?1 AND BUSINESSTYPE = ?2 "
				+ "FOR UPDATE";
		Query query = _em.createNativeQuery(sql, IssueFeePolicyLimit.class);
		query.setParameter(1, issueCompnyNo);
		query.setParameter(2, businessType);
		List<IssueFeePolicyLimit> list = query.getResultList();
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}


	/**
	 * 总记录数
	 * @param issueCompnyNo
	 * @return
	 */
	@Override
	public BigInteger getIssueFeePolicyLimitListTotal(String issueCompnyNo, String businessType) {
		String sql = "SELECT NVL(COUNT(*),0) FROM ISSUEFEEPOLICYLIMIT T WHERE T.ISSUECOMPANYNO = ?1 ";
		if(!PCPSUtil.isEmpty(businessType)){
			sql = sql + "AND I.BUSINESSTYPE = '" + businessType + "'";
		}
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, issueCompnyNo);
		return new BigInteger(query.getSingleResult().toString());
	}

	@SuppressWarnings("unchecked")
	@Override
	public IssueFeePolicyLimit getIssueFeePolicyLimitByCompNoAndTypeForEffective(
			String issueCompnyNo, String businessType) {
		String sql = "SELECT I.ISSUECOMPANYNO, I.BUSINESSTYPE, I.FEEUPPERLIMIT, "
			+ "I.FEELOWERLIMIT, I.OPERID, I.MODIFYTIME, I.REMARK, I.STATUS, I.LASTAUTHOPERID, I.LASTAUTHTIME, I.AUTHREMARK "
			+ "FROM ISSUEFEEPOLICYLIMIT I "
			+ "WHERE I.ISSUECOMPANYNO = ?1 AND BUSINESSTYPE = ?2 AND I.STATUS = '1'";
	Query query = _em.createNativeQuery(sql, IssueFeePolicyLimit.class);
	query.setParameter(1, issueCompnyNo);
	query.setParameter(2, businessType);
	List<IssueFeePolicyLimit> list = query.getResultList();
	if (list.size() > 0) {
		return list.get(0);
	}
	return null;
	}

	@Override
	public Integer getIssueFeePolicyLimitForNotEffective(String issueCompnyNo) {
		String sql = "SELECT NVL(COUNT(*),0) FROM ISSUEFEEPOLICYLIMIT T WHERE T.ISSUECOMPANYNO = ?1 AND T.STATUS = '0'";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, issueCompnyNo);
		return Integer.parseInt(query.getSingleResult().toString());
	}

}
